package com.bw.dichotomy;

public class Dichotomys02 {
    /**
     * 二分法的前提是    已经排好序的一组数
     * @param array
     * @param value
     * @return
     */
    private static int findDicjotomys(int[] array, int value) {
        int low = 0;
        int high = array.length - 1;
        while(low <= high) {
            System.out.println(value);
            int middle = (low + high) / 2;
            System.out.println(array[middle]);
            if(value ==  array[middle]) {
                System.out.println("----1");
                return middle;
            }
            if(value > array[middle]) {
                System.out.println("----2");
                low = middle + 1;
            }
            if(value < array[middle]) {
                System.out.println("----3");
                high = middle - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int arr[] = {1,6,5,8,19,9,8,2,6,8,7,7,2};
        int value = 9;
        int i = findDicjotomys(arr, value);
        System.out.println("查询的结果为："+i);

    }

}
